﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace Redmine.Laptop.Common.Settings.SettingsUtilities
{
    public static partial class CommonSettings
    {
        public static class ReportSettings
        {
            /// <summary>
            /// Default developers working hours per week
            /// </summary>
            private const int DefaultWorkingHoursPerWeek = 40;

            /// <summary>
            /// Get all non commercial projects
            /// </summary>
            /// <returns></returns>
            public static IEnumerable<string> NonCommercialProjectNames
            {
                get { return Document.Descendants("Project").Select(p => p.Value); }
                set
                {
                    Document.Descendants("Project").Remove();
                    Document.Descendants("Projects").First().Add(value.Select(v => new XElement("Project", v)));
                }
            }

            /// <summary>
            /// Get all developers name
            /// </summary>
            /// <returns></returns>
            public static IEnumerable<string> DevelopersNames
            {
                get { return Document.Descendants("Developer").Select(d => d.Value); }
                set
                {
                    Document.Descendants("Developer").Remove();
                    Document.Descendants("Developers").First().Add(value.Select(v => new XElement("Developer", v)));
                    Document.Save(XmlSettingsPath);
                }
            }

            /// <summary>
            /// Get tuples of developer name and his working hours per week
            /// </summary>
            public static IEnumerable<Tuple<string, int>> DevelopersWorkingHoursPerWeek
            {
                get
                {
                    return
                        from d in Document.Descendants("Developer")
                        let attribute = d.Attribute("workingHoursPerWeek")
                        select Tuple.Create
                            (
                                d.Value,
                                attribute != null ? Convert.ToInt32(attribute.Value) : DefaultWorkingHoursPerWeek
                            );
                }
            }
        }
    }
}
